package com.ww.scm.repository.mat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ww.core.orm.PropertyFilter;
import com.ww.core.orm.PropertyFilter.MatchType;
import com.ww.core.orm.hibernate.HibernateDao;
import com.ww.core.utils.DateUtils;
import com.ww.scm.constants.Constants;
import com.ww.scm.entity.mat.Bill;

//Spring Bean的标识.
@Component
public class BillRepository extends HibernateDao<Bill, String>{
	
	public void delete(Bill bill,String operator) {
		Date nowDate=new Date();
		bill.setSysFlag(Constants.SYS_FLAG_DELETED);
		bill.setLastModifier(operator);
		bill.setLastModifiedTime(DateUtils.formatDateHms(nowDate));
		getSession().save(bill);
	}
	
	public void delete(String id,String operator) {
		Bill bill=get(id);
		Date nowDate=new Date();
		bill.setSysFlag(Constants.SYS_FLAG_DELETED);
		bill.setLastModifier(operator);
		bill.setLastModifiedTime(DateUtils.formatDateHms(nowDate));
		save(bill);
	}
	
	public synchronized int getNextSequence(String billType,String billDate) {
		List<PropertyFilter> filters=new ArrayList<PropertyFilter>();
		filters.add(new PropertyFilter(MatchType.EQ,"billType.id",billType));
		filters.add(new PropertyFilter(MatchType.EQ,"billDate",billDate));
		List<Bill> billList=find(filters);
		return billList.size()+1;
	}
}
